package com.kuang.demo.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuang.demo.bean.acgBeanVo.BaseData;
import com.kuang.demo.bean.lhf.Coupon;
import com.kuang.demo.bean.lhf.CouponExample;
import com.kuang.demo.mapper.CouponMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CouponServiceImp implements CouponService{

    @Autowired
    CouponMapper couponMapper;

    @Override
    public BaseData queryCouponList(Integer page, Integer limit, String sort, String order, String name, Integer type, Integer status) {
        //预编译的时候增加分页的sql语句
        PageHelper.startPage(page,limit);
        //设置排序的条件
        CouponExample couponExample = new CouponExample();
        couponExample.setOrderByClause(sort+" "+order);
        //设置查询条件
        if (name != null && !"".equals(name)){
            couponExample.createCriteria().andNameLike("%"+name+"%");
        }
        if (type != null){
            couponExample.createCriteria().andTypeEqualTo(type.shortValue());
        }
        if (status != null){
            couponExample.createCriteria().andStatusEqualTo(status.shortValue());
        }
        //获取查询结果
        List<Coupon> coupons = couponMapper.selectByExample(couponExample);
        //可以在查询完成后获得pageInfo → 根据查询结果获得总条目数total
        PageInfo<Coupon> adPageInfo = new PageInfo<>(coupons);
        long total = adPageInfo.getTotal();

        //所需数据封装并返回
        return BaseData.data(coupons,total);
    }

    @Override
    public Coupon queryCouponRead(Integer id) {
        Coupon coupon = couponMapper.selectByPrimaryKey(id);
        return coupon;
    }

    @Override
    public BaseData queryCouponListUser(Integer id, Integer limit, Integer couponId, String add_time, String order) {

        BaseData baseDatalhf = new BaseData();
        baseDatalhf.setTotal(0);
        ArrayList<Object> items = new ArrayList<>();
        baseDatalhf.setItems(items);
        return baseDatalhf;
    }

    @Override
    public void updateCoupon(Coupon coupon) {
        CouponExample couponExample = new CouponExample();
        couponExample.createCriteria().andIdEqualTo(coupon.getId());
        couponMapper.updateByExample(coupon,couponExample);
    }

    @Override
    public void deleteCoupon(Coupon coupon) {
        couponMapper.deleteByPrimaryKey(coupon.getId());
    }

    @Override
    public Coupon createCoupon(Coupon coupon) {
        coupon.setAddTime(new Date());
        coupon.setUpdateTime(new Date());
        coupon.setDeleted(false);
        int insert = couponMapper.insert(coupon);
        return coupon;
    }

}
